import React from 'react'
import { Progress } from 'antd';
// ===================================================================== Temp
import FormModal from '#cpt/temp/FormModal'
// ===================================================================== global declare
const { $fn, $http } = window
// ===================================================================== Lazy
// ===================================================================== component
class Index extends React.Component{
	state = {
		data:[],
		pag: {},
		selectedKeys: [],
		current: 1,
		status: true,
		percent: 0,
		okText: '暂停'
	}
	
	model = {}

	componentDidMount(){
		this.props.onRef?.(this)
	}
	open = ({api,date,type})=>{
		this.api = api
		this.date = date
		this.type = type
		const restInfo = $fn.local(`${this.api}`)
		let cur = 1
		let per = 0
		if(this.type===1){
			this.param = {reset_date:this.date}
			if(restInfo){
				const { current, reset_date, percent } = restInfo
				if(reset_date === date){
					cur = current
					per = percent
				}
			}
		} else {
			this.param = {summary_sn:this.date}
			if(restInfo){
				const { current, summary_sn, percent } = restInfo
				if(summary_sn === date){
					cur = current
					per = percent
				}
			}
		}
		this.setState({status:true,current:cur,okText:'暂停', percent:per},()=>this.reset())
		this.modal.open({ formRef:this.formRef })
	}
	reset = () => {
		const { current, status } = this.state
		if(status){
			$http.submit(null,this.api,{param:{page:current, limit:100,...this.param},loading:false}).then(data=>{
				const percent = (data.current / data.total_pages * 100).toFixed(2)
				this.setState({current:current+1,percent})
				if(+data.current<+data.total_pages){
					this.reset()
				}
			})
		}
	}
	
	render(){
		let { okText, percent, status,current } = this.state
		if(+percent === 100){
			this.modal.close()
			$fn.remove(`${this.api}`)
		}
		return (
			<>
                {/* 重置数据 */}
				{FormModal.call(this,{ title:'生成/重置数据', labelWidth:'s5', width:300, noCancel:true, okText,
					onOk:()=>{
						status = !status
						okText = status ? '暂停' : '开始'
						if(!status){
							$fn.local(`${this.api}`,{...this.param,current,percent} )
						}
						this.setState({status,okText},()=>this.reset())
					},
					onClose:()=>{
						status = false
						if(!status){
							$fn.local(`${this.api}`,{...this.param,current,percent} )
						}
						this.setState({status},()=>this.reset())
					},
					content:<div className='mb20 mr20'>
						<Progress percent={percent} />
					</div>
				})}
			</>
		)
	}
}

export default Index